'use strict';
const axios = require('axios')
const cheerio = require("cheerio")
exports.main = async (event, context) => {
	const db = uniCloud.database();
	const mdb = db.collection('movie')
	const dbCmd = db.command
	
	let res = await mdb.field({'aHref':true}).where({order:dbCmd.gt(247).and(dbCmd.lt(252))}).get()  //获得仅包含a连接地址的对象
	let data = res.data
	// console.log(data.length)
	for(let i=0;i<data.length;i++){
		let commentList = []
		
			await axios.get(data[i].aHref).then(res=>{
			let $ = cheerio.load(res.data)
			let t = ""
			if ($('#link-report .all').text() != '') {
			    t = $('#link-report .all').text()
			  }
			  else {
			    t = $('#link-report .pl').prev().text()
			  }
				
				//获取热评
				$('#hot-comments .comment').each((i,e)=>{
					let c = {
						userName: "",
						dateTime: "",
						comment: "",
						rate: 0
					}
					c.userName = $(e).find('.comment-info a').text()
					let rate = $(e).find('.comment-info .rating').attr('class')
					if(rate != undefined){
					  rate = parseInt(rate.match(/[0-9]/g).join('')) 
					  c.rate = rate
					}
					let dateTime = $(e).find('.comment-info .comment-time ').text()
					dateTime = dateTime.replace(/\n/g,'')
					c.dateTime = dateTime
					c.comment = $(e).find('.short').text()
					commentList.push(c)
				}) 
			 mdb.doc(data[i]._id).update({"content":t,"comment":commentList})
		})
	}
	// let content = "www.baidu.com"
	// let res= await mdb.where({title:"zhansan"}).update({"b":content})  //根据字段值匹配更新
	// let res = await mdb.doc("617e5f9396cef20001d68a17").update({"a":"movie.douban.com"})  //根据_id值匹配更新
	// return data[0].aHref
};
